package com.dragon.mmochat.service;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.message.Mmochat_8_11C;
import com.dragon.mmochat.message.Mmochat_8_1C;
import com.dragon.mmochat.message.Mmochat_8_4C;
import com.dragon.mmochat.message.Mmochat_8_5C;
import com.dragon.mmochat.message.Mmochat_8_6C;
import com.dragon.mmochat.message.Mmochat_8_7C;
import com.dragon.mmochat.message.Mmochat_8_8C;
import com.dragon.mmochat.message.Mmochat_8_9S;
import com.dragon.mmochat.model.MmochatTeam;
import com.dragon.mmochat.model.enumType.MmochatRoleStateType;
import com.dragon.mmochat.model.enumType.MmochatTeamStateType;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.util.CreateTlvWin;
import com.dragon.mmochat.util.MmochatUtil;
import com.skymobi.handler.SkymobiHandler;
import com.skymobi.message.SkymobiProtocolMessage;
import com.skymobi.message.widget.AddDataTLV;
import com.skymobi.message.widget.ClearDataTLV;
import com.skymobi.message.widget.CloseWinTLV;
import com.skymobi.message.widget.CreateMsgBoxTLV;
import com.skymobi.message.widget.SendDataTLV;
import com.skymobi.message.widget.ShowPopupMenuTLV;
import com.skymobi.message.widget.ShowWaitingMsgBoxTLV;
import com.skymobi.message.widget.WinType;
import com.skymobi.service.SkymobiService;
import com.skymobi.util.Text;

public class MmochatTeamService {
	private static Logger log = LoggerFactory
			.getLogger(MmochatTeamService.class);
	public static Map<Integer, MmochatTeam> teams = new ConcurrentHashMap<Integer, MmochatTeam>();

	// 返回角色组队状态
	public static MmochatTeamStateType getTeamState(MmochatPlayer me) {
		if (me == null) {
			return MmochatTeamStateType.没在队中;
		}
		if (!me.isInTeam()) {
			return MmochatTeamStateType.没在队中;
		} else {
			MmochatTeam team = teams.get(me.getTeamId());
			if (team == null) {
				me.leaveTeam();
				return MmochatTeamStateType.没在队中;
			} else {
				if (team.isTeamLeader(me.getRoleId())) {
					return MmochatTeamStateType.队长;
				} else {
					return MmochatTeamStateType.队员;
				}
			}
		}
	}

	public static List<MmochatPlayer> getMyTeamMember(MmochatPlayer me) {
		List<MmochatPlayer> teamers = new ArrayList<MmochatPlayer>();
		if (me == null) {
			return teamers;
		}
		if (me.isInTeam()) {
			MmochatTeam team = MmochatTeamService.teams.get(me.getTeamId());
			if (team == null) {
				me.leaveTeam();
				teamers.add(me);
			} else {
				for (Integer roleId : team.getMemberId().values()) {
					MmochatPlayer member = MmochatMainService.players
							.get(roleId);
					if (member != null) {
						teamers.add(member);
					}
				}
			}
		} else {
			teamers.add(me);
		}
		return teamers;
	}

	public static void notifyTeamStateToClient(MmochatPlayer me) {
		Mmochat_8_9S pack = new Mmochat_8_9S();
		pack.setTeamState(MmochatTeamService.getTeamState(me));
		MmochatUtil.sendCommonPack(me, pack);
	}

	// 查看我的队伍
	public static CreateTlvWin getMyTeamInfo(MmochatPlayer me) {
		if (!me.isInTeam()) {
			return null;
		}
		MmochatTeam team = teams.get(me.getTeamId());
		if (team == null) {
			return null;
		}
		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.List);
		tlv.setRightName("返回");
		tlv.setTitle("我的队伍");
		tlv.setWinHeight(180);
		tlv.setWinId(MmochatConstant.TLV_WIN_MYTEAM);
		tlv.addRightTLV(new CloseWinTLV(MmochatConstant.TLV_WIN_MYTEAM));
		for (Integer roleId : team.getMemberId().values()) {
			MmochatPlayer member = MmochatMainService.players.get(roleId);
			if (member != null) {
				String item = "";
				Color color = Color.cyan;
				if (member.isVip()) {
					color = Color.yellow;
				}
				// if (team.isTeamLeader(roleId)) {
				// color = Color.green;
				// }
				item += member.getName();
				item += "\n" + member.getLevel() + "级,";
				item += member.getFamily().getName();
				if (member.getMapId() == MmochatConstant.mapId_跨服擂台) {
					item += "," + member.getPk_score() + "竞技点";
				}
				item = MmochatUtil.wrapColor(item, color);
				int bmpId;
				if (team.isTeamLeader(roleId)) {
					bmpId = MmochatConstant.BMP_TEAMTAG;
				} else {
					bmpId = member.getBmpId();
				}
				tlv.addItemEvent(bmpId, item, new ClearDataTLV(),
						new AddDataTLV((int) roleId));
			}
		}

		if (team.isTeamLeader(me.getRoleId())) {
			tlv.setLeftName("菜单");

			ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
			wait.setShowCancel(false);
			ShowPopupMenuTLV menu = new ShowPopupMenuTLV();
			menu.addItemEvent("踢出队伍", new SendDataTLV(MmochatConstant
					.getClientModuleId(), MmochatMain.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_8_5, me.getRoleId()),
					wait);
			menu.addItemEvent("加为好友", new AddDataTLV((int) me.getRoleId()),
					new SendDataTLV(MmochatConstant.getClientModuleId(),
							MmochatMain.getModuleInstID(),
							MmochatConstant.MSGCODE_MMOCHAT_C_6_1, (int) 5),
					new ShowWaitingMsgBoxTLV());
			// menu.addItemEvent("入队请求", new ClearDataTLV(), new SendDataTLV(
			// MmochatConstant.getClientModuleId(), MmochatMain
			// .getModuleInstID(),
			// MmochatConstant.MSGCODE_MMOCHAT_C_8_6, me.getRoleId()),
			// new ShowWaitingMsgBoxTLV());
			menu.addItemEvent("转让队长", new SendDataTLV(MmochatConstant
					.getClientModuleId(), MmochatMain.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_8_7, me.getRoleId()),
					wait);
			menu.addItemEvent("解散队伍", new ClearDataTLV(), new SendDataTLV(
					MmochatConstant.getClientModuleId(), MmochatMain
							.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_8_4, me.getRoleId()),
					wait);
			tlv.addLeftTLV(menu);
		} else {
			tlv.setLeftName("菜单");
			ShowPopupMenuTLV menu = new ShowPopupMenuTLV();

			menu.addItemEvent("加为好友", new AddDataTLV((int) me.getRoleId()),
					new SendDataTLV(MmochatConstant.getClientModuleId(),
							MmochatMain.getModuleInstID(),
							MmochatConstant.MSGCODE_MMOCHAT_C_6_1, (int) 5),
					new ShowWaitingMsgBoxTLV());

			ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
			wait.setShowCancel(false);
			menu.addItemEvent("离队", new ClearDataTLV(), new SendDataTLV(
					MmochatConstant.getClientModuleId(), MmochatMain
							.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_8_4, me.getRoleId()),
					wait);
			tlv.addLeftTLV(menu);
		}
		return tlv;
	}

	// 8.1 创建队伍/查看队伍信息
	@SkymobiService
	public SkymobiProtocolMessage handleC_8_1(SkymobiHandler handler,
			Mmochat_8_1C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		if (me.isInTeam()) {
			// 已组队
			MmochatTeam team = teams.get(me.getTeamId());
			if (team == null) {
				me.leaveTeam();
			} else {
				CreateTlvWin teamInfoTlv = getMyTeamInfo(me);
				if (team.isTeamLeader(me.getRoleId())) {
					CreateTlvWin requesttlv = getTeamRequest(me, team);
					if (requesttlv != null) {
						return MmochatUtil.tlvResponse(req.getHallState(),
								new CloseWinTLV(
										MmochatConstant.TLV_WIN_TEAMREQUEST),
								requesttlv);
					}
				}
				return MmochatUtil.tlvResponse(req.getHallState(),
						new CloseWinTLV(MmochatConstant.TLV_WIN_TEAMREQUEST),
						teamInfoTlv);
			}
		}
		if (!me.isInTeam() && !me.isInBattle()) {
			// 创建队伍
			if (me.lockState(MmochatRoleStateType.创建队伍时)) {
				if (me.isInTeam() || me.isInBattle()) {
					me.unLockState();
					return null;
				}
				new MmochatTeam(me);
				me.unLockState();
			}
			return null;
		}
		return null;
	}

	// 8.4 离队
	@SkymobiService
	public SkymobiProtocolMessage handleC_8_4(SkymobiHandler handler,
			Mmochat_8_4C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		if (!me.isInTeam()) {
			return MmochatUtil.msgbox(req.getHallState(), "你不在队伍中!");
		}
		if (me.isInBattle()) {
			return null;
		}
		if (!me.lockState(MmochatRoleStateType.离队时)) {
			return null;
		}
		if (me.isInBattle()) {
			return null;
		}
		MmochatTeam team = teams.get(me.getTeamId());
		if (team == null) {
			me.leaveTeam();
		} else {
			if (team.isTeamLeader(me.getRoleId())) {
				team.dismissTeam();
			} else {
				team.delMember(me.getRoleId());
			}
		}
		me.unLockState();
		return MmochatUtil.tlvResponse(req.getHallState(), new CloseWinTLV(
				MmochatConstant.TLV_WIN_MYTEAM));
	}

	// 8.5 踢出队伍
	@SkymobiService
	public SkymobiProtocolMessage handleC_8_5(SkymobiHandler handler,
			Mmochat_8_5C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		MmochatTeam team = teams.get(me.getTeamId());
		if (team == null) {
			me.leaveTeam();
			return MmochatUtil.msgbox(req.getHallState(), "你所在的队伍已经解散了!");
		} else {
			if (team.isTeamLeader(me.getRoleId())) {
				if (req.getTargetRoleId() == me.getRoleId()) {
					return MmochatUtil.msgbox(req.getHallState(), "无法踢出队长!");
				} else {
					// 下发通知
					boolean success = team.delMember(req.getTargetRoleId());
					if (success) {
						MmochatPlayer he = MmochatMainService.players.get(req
								.getTargetRoleId());
						if (he != null) {
							MmochatChatService.sendSystemMsgToPerson(he,
									"你被请离了队伍!");
						}
					}
					CreateTlvWin tlv = getMyTeamInfo(me);
					return MmochatUtil.tlvResponse(req.getHallState(),
							new CloseWinTLV(MmochatConstant.TLV_WIN_MYTEAM),
							tlv);
				}
			} else {
				return MmochatUtil.msgbox(req.getHallState(), "你不是队长!无法进行此操作!");
			}
		}
	}

	// 查看入队请求
	public static CreateTlvWin getTeamRequest(MmochatPlayer me, MmochatTeam team) {
		if (me == null || team == null) {
			return null;
		}
		team.clearDeadRequest();
		if (team.getRequestId().size() == 0) {
			return null;
		}
		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.List);
		tlv.setLeftName("菜单");
		tlv.setRightName("返回");
		tlv.setTitle("入队请求");
		tlv.setWinHeight(180);
		tlv.setWinId(MmochatConstant.TLV_WIN_TEAMREQUEST);
		tlv.addRightTLV(new CloseWinTLV(MmochatConstant.TLV_WIN_TEAMREQUEST));

		for (Integer roleId : team.getRequestId().values()) {
			MmochatPlayer player = MmochatMainService.players.get(roleId);
			if (player != null) {
				String item = player.getName();
				if (player.isVip()) {
					item = MmochatUtil.wrapColor(item, Color.yellow);
				}
				item += "\n" + player.getLevel() + "级,";
				item += player.getFamily().getName();

				tlv.addItemEvent(player.getBmpId(), item, new ClearDataTLV(),
						new AddDataTLV((int) roleId));
			}
		}

		ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
		wait.setShowCancel(false);
		ShowPopupMenuTLV menu = new ShowPopupMenuTLV();
		menu.addItemEvent("同意", new AddDataTLV((int) me.getRoleId()),
				new SendDataTLV(MmochatConstant.getClientModuleId(),
						MmochatMain.getModuleInstID(),
						MmochatConstant.MSGCODE_MMOCHAT_C_8_8, 0), wait);
		menu.addItemEvent("拒绝", new AddDataTLV((int) me.getRoleId()),
				new SendDataTLV(MmochatConstant.getClientModuleId(),
						MmochatMain.getModuleInstID(),
						MmochatConstant.MSGCODE_MMOCHAT_C_8_8, 1), wait);
		menu.addItemEvent("清空列表", new AddDataTLV((int) me.getRoleId()),
				new SendDataTLV(MmochatConstant.getClientModuleId(),
						MmochatMain.getModuleInstID(),
						MmochatConstant.MSGCODE_MMOCHAT_C_8_8, 2), wait);
		tlv.addLeftTLV(menu);
		return tlv;
	}

	// 8.6 查看入队请求
	@SkymobiService
	public SkymobiProtocolMessage handleC_8_6(SkymobiHandler handler,
			Mmochat_8_6C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		MmochatTeam team = teams.get(me.getTeamId());
		if (team == null) {
			me.leaveTeam();
			return MmochatUtil.msgbox(req.getHallState(), "你所在的队伍已经解散了!");
		} else {
			if (team.isTeamLeader(me.getRoleId())) {
				if (team.isFull()) {
					// 人满，清空入队请求，显示队伍信息
					team.getRequestId().clear();
					CreateTlvWin teamInfoTlv = getMyTeamInfo(me);
					return MmochatUtil
							.tlvResponse(req.getHallState(), new CloseWinTLV(
									MmochatConstant.TLV_WIN_TEAMREQUEST),
									teamInfoTlv);
				}
				CreateTlvWin tlv = getTeamRequest(me, team);
				if (tlv == null) {
					return MmochatUtil.msgbox(req.getHallState(), "没有申请入队的人!");
				}
				return MmochatUtil.tlvResponse(req.getHallState(),
						new CloseWinTLV(MmochatConstant.TLV_WIN_TEAMREQUEST),
						tlv);
			} else {
				return MmochatUtil.msgbox(req.getHallState(), "你不是队长!无法进行此操作!");
			}
		}
	}

	// 8.7 转让队长
	@SkymobiService
	public SkymobiProtocolMessage handleC_8_7(SkymobiHandler handler,
			Mmochat_8_7C req) {

		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		MmochatTeam team = teams.get(me.getTeamId());
		if (team == null) {
			me.leaveTeam();
			return MmochatUtil.msgbox(req.getHallState(), "你所在的队伍已经解散了!");
		} else {
			if (team.isTeamLeader(me.getRoleId())) {
				if (me.getRoleId() == req.getTargetRoleId()) {
					return MmochatUtil.msgbox(req.getHallState(), "你已经是队长了!");
				}
				MmochatPlayer newLeader = MmochatMainService.players.get(req
						.getTargetRoleId());
				if (newLeader == null || newLeader.isDeadConnection()) {
					return MmochatUtil.msgbox(req.getHallState(),
							"对方已经掉线,无法接受队长!");
				}
				if (!newLeader.isInTeam()
						|| newLeader.getTeamId() != me.getTeamId()) {
					return MmochatUtil.msgbox(req.getHallState(), "对方已经离开队伍!");
				}

				if (me.isInBattle()) {
					return null;
				}

				if (!me.lockState(MmochatRoleStateType.转让队长时)) {
					return null;
				}
				if (me.isInBattle()) {
					me.unLockState();
					return null;
				}
				if (!newLeader.lockState(MmochatRoleStateType.转让队长时)) {
					me.unLockState();
					return MmochatUtil
							.msgbox(req.getHallState(), "对方现在无法接受队长!");
				}
				if (newLeader.isInBattle()) {
					newLeader.unLockState();
					me.unLockState();
					return MmochatUtil
							.msgbox(req.getHallState(), "对方现在无法接受队长!");
				}
				team.setTeamLeaderRoleId(newLeader.getRoleId());
				newLeader.unLockState();
				me.unLockState();

				notifyTeamStateToClient(newLeader);
				notifyTeamStateToClient(me);

				// 下发队伍变更消息,图标等变动,消息通知
				for (Integer roleId : team.getMemberId().values()) {
					MmochatPlayer member = MmochatMainService.players
							.get(roleId);
					if (member != null) {
						// 向此玩家下发信息
						if (member.getRoleId() == newLeader.getRoleId()) {
							MmochatChatService.sendSystemMsgToPerson(member,
									"你已成为新的队长!");
						} else {
							MmochatChatService.sendSystemMsgToPerson(member,
									"队长已转移给" + newLeader.getName() + "!");
						}
					}
				}

				return MmochatUtil.msgbox(req.getHallState(), "转让成功!");
			} else {
				return MmochatUtil.msgbox(req.getHallState(), "你不是队长!无法进行此操作!");
			}
		}
	}

	// 8.8 处理入队请求
	@SkymobiService
	public SkymobiProtocolMessage handleC_8_8(SkymobiHandler handler,
			Mmochat_8_8C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		MmochatTeam team = teams.get(me.getTeamId());
		if (team == null) {
			me.leaveTeam();
			return MmochatUtil.msgbox(req.getHallState(), "你所在的队伍已经解散了!");
		} else {
			if (team.isTeamLeader(me.getRoleId())) {
				switch (req.getDealType()) {
				case 0:
					// 同意
					if (team.isFull()) {
						return MmochatUtil
								.msgbox(req.getHallState(), "队伍人数已满!");
					}
					MmochatPlayer target = MmochatMainService.players.get(req
							.getTargetRoleId());
					if (target == null) {
						return MmochatUtil
								.msgbox(req.getHallState(), "对方已经掉线!");
					}
					if (target.isInTeam()) {
						team.delRequest(target.getRoleId());
						return MmochatUtil.msgbox(req.getHallState(),
								"对方已经在队伍里了!");
					}
					if (target.isInBattle()) {
						return MmochatUtil.msgbox(req.getHallState(),
								"对方正在战斗中,暂时无法加入队伍!");
					}
					if (!target.isFreeState()) {
						return MmochatUtil.msgbox(req.getHallState(),
								"对方现在正忙,暂时无法加入队伍!");
					}
					if (me.lockState(MmochatRoleStateType.批准他人入队时)) {
						if (target.isInTeam()) {
							me.unLockState();
							team.delRequest(target.getRoleId());
							return MmochatUtil.msgbox(req.getHallState(),
									"对方已经在队伍里了!");
						}
						if (target.isInBattle()) {
							me.unLockState();
							return MmochatUtil.msgbox(req.getHallState(),
									"对方正在战斗中,暂时无法加入队伍!");
						}
						if (team.isFull()) {
							me.unLockState();
							return MmochatUtil.msgbox(req.getHallState(),
									"队伍人数已满!");
						}
						team.delRequest(target.getRoleId());
						team.addMember(target.getRoleId());
						me.unLockState();
					}
					break;
				case 1:
					// 拒绝
					team.delRequest(req.getTargetRoleId());
					// TODO:发消息通知,请求被拒绝
					MmochatPlayer he = MmochatMainService.players.get(req
							.getTargetRoleId());
					if (he != null) {
						if (!he.isInTeam()) {
							MmochatChatService.sendSystemMsgToPerson(he, me
									.getName()
									+ "拒绝了您的入队请求!");
						}
					}
					break;
				case 2:
					// 清空
					team.getRequestId().clear();
					break;
				default:
					break;
				}
				CreateTlvWin tlv = getTeamRequest(me, team);
				if (tlv == null) {
					CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
					msgbox.setContent(new Text("所有请求已经处理完成!"));
					msgbox.setLeftName("确定");
					return MmochatUtil
							.tlvResponse(req.getHallState(), new CloseWinTLV(
									MmochatConstant.TLV_WIN_TEAMREQUEST));
				}
				return MmochatUtil.tlvResponse(req.getHallState(),
						new CloseWinTLV(MmochatConstant.TLV_WIN_TEAMREQUEST),
						tlv);
			} else {
				return MmochatUtil.msgbox(req.getHallState(), "你不是队长!无法进行此操作!");
			}
		}
	}

	// 处理组队邀请
	@SkymobiService
	public SkymobiProtocolMessage handleC_8_11(SkymobiHandler handler,
			Mmochat_8_11C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		if (req.getIndex() == 1) {
			// 拒绝
			MmochatPlayer teamLeader = MmochatMainService.players.get(req
					.getTeamLeaderRoleId());
			if (teamLeader != null
					&& MmochatTeamService.getTeamState(teamLeader) == MmochatTeamStateType.队长) {
				MmochatTeam team = MmochatTeamService.teams.get(teamLeader
						.getTeamId());
				if (team != null && !team.isFull()) {
					MmochatChatService.sendSystemMsgToPerson(teamLeader,
							MmochatUtil.wrapColor(me.getName(), Color.green)
									+ "拒绝了您的入队邀请!");
					return null;
				}
			}
		} else if (req.getIndex() == 0) {
			// 已经在队中了，不再处理；如果在战斗中等其它情况下无法进队的，不再处理
			if (me.isInTeam()) {
				return null;
			}
			if (me.isInBattle()) {
				return null;
			}
			if (!me.isFreeState()) {
				return null;
			}

			// 发起方的队伍是否还在、是否已满、是否在战斗中，队长是否仍然是发起方
			MmochatPlayer teamLeader = MmochatMainService.players.get(req
					.getTeamLeaderRoleId());
			if (teamLeader == null) {
				MmochatChatService.sendSystemMsgToPerson(me, "对方已经下线，加入队伍失败!");
				return null;
			}
			if (teamLeader.getMapId() != me.getMapId()
					|| teamLeader.getLine() != me.getLine()) {
				MmochatChatService.sendSystemMsgToPerson(me, "对方已经离开，加入队伍失败!");
				return null;
			}

			MmochatTeam team = MmochatTeamService.teams.get(teamLeader
					.getTeamId());
			if (team == null) {
				MmochatChatService.sendSystemMsgToPerson(me, "对方队伍已解散，加入队伍失败!");
				return null;
			}
			if (team.isFull()) {
				MmochatChatService.sendSystemMsgToPerson(me, "对方队伍已满，加入队伍失败!");
				return null;
			}
			if (team.getTeamLeaderRoleId() != teamLeader.getRoleId()) {
				MmochatChatService
						.sendSystemMsgToPerson(me, "对方已经不是队长，加入队伍失败!");
				return null;
			}
			if (teamLeader.isInBattle()) {
				MmochatChatService.sendSystemMsgToPerson(me, "对方正在战斗中，加入队伍失败!");
				return null;
			}

			// 进入队伍
			if (teamLeader.lockState(MmochatRoleStateType.批准他人入队时)) {
				if (me.isInTeam()) {
					teamLeader.unLockState();
					return null;
				}
				if (me.isInBattle()) {
					teamLeader.unLockState();
					return null;
				}
				if (team.isFull()) {
					teamLeader.unLockState();
					MmochatChatService.sendSystemMsgToPerson(me,
							"对方队伍已满，加入队伍失败!");
					return null;
				}
				team.addMember(me.getRoleId());
				teamLeader.unLockState();
			}
		}
		return null;
	}
}
